
import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet implementation class for Servlet: UpdateItem
 *
 */
 public class UpdateItem extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   
	protected Connection con;
	protected String dbsource = "jdbc:mysql://localhost/inventory"; 
	protected String drv = "com.mysql.jdbc.Driver";
	protected String defaultquery = "select * from item";
	protected PreparedStatement ps;
	protected Statement s;
	protected ResultSet rs;
	protected String uvar = null;
	PrintWriter out;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		out = response.getWriter();

		
		out.print("<html><body>");
		
		if(request.getParameter("itemcnt") != null)//check if url variable "itemcnt" exists
		{
			uvar = request.getParameter("itemcnt").trim();
			doForm(request, response);
		}
		else
			out.print("Error: No passed variable in url.<br/><a href=\"http://localhost:8080/finals/InventoryList\">Back</a>");
		
		out.print("</body></html>");
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("updateitem") != null)
			doUpdate(request,response);
		else
			response.sendRedirect("http://localhost:8080/finals/InventoryList");
	}  
	
	protected void doForm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		out = response.getWriter();
		
		String s1 = null;
		String s2 = null;
		String s3 = null;
		String s4 = null;
		String s5 = null;

		try
		{
			Class.forName(drv);
			con = DriverManager.getConnection(dbsource, "root", "");
			
			s = con.createStatement();
			rs = s.executeQuery(defaultquery);
			

			while(rs.next()) 
			{
				s1 = rs.getString(1).trim();//item table PK
				s2 = rs.getString(2).trim();
				s3 = rs.getString(3).trim();
				s4 = rs.getString(4).trim();
				s5 = rs.getString(5).trim();
				
				
				if (s1.equals(uvar))
				{
					out.print("<form ");
					out.print(" method=\"post\">");
					out.print("Item Code :");
					out.print("<input type=\"text\" value=\"");
					out.print(s1);
					out.print("\" name=\"itemCode\"><br>");
					out.print("Item Name :");
					out.print("<input type=\"text\" value=\"");
					out.print(s2);
					out.print("\" name=\"itemName\"><br>");
					out.print("Item Detail :");
					out.print("<input type=\"text\" value=\"");
					out.print(s3);
					out.print("\" name=\"itemDetail\"><br>");
					out.print("Category Code :");
					out.print("<input type=\"text\" value=\"");
					out.print(s4);
					out.print("\" name=\"categoryCode\"><br>");
					out.print("Quantity :");
					out.print("<input type=\"text\" value=\"");
					out.print(s5);
					out.print("\" name=\"itemQuantity\"><br>");
					out.print("<br><br><input type=\"submit\" name=\"updateitem\" value=\"Update Item\">");
					out.print("<input type=\"reset\" value=\"Reset\"></form><br/><a href=\"http://localhost:8080/finals/InventoryList\">Back</a>");
					
					break;
				}
				
				
				
			}
			
			if (!s1.equals(uvar))
			{
				out.print("Row does not exist or no variable passed in url.<br/><a href=\"http://localhost:8080/finals/InventoryList\">Back</a>");
			}
			
			s.close();
			con.close();
			out.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	protected void doUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		try
		{
			Class.forName(drv);
			con = DriverManager.getConnection(dbsource, "root", "");
			

			
			ps = con.prepareStatement("update item set item_code=?, item_name=?, item_detail=?, category_code=?, quantity=? where item_code=?");
			ps.setString(1, request.getParameter("itemCode"));
			ps.setString(2, request.getParameter("itemName"));
			ps.setString(3, request.getParameter("itemDetail"));
			ps.setString(4, request.getParameter("categoryCode"));
			ps.setString(5, request.getParameter("itemQuantity"));
			ps.setString(6, uvar);
			int ex = ps.executeUpdate();
			System.out.println("Update Success!");
			
			ps.close();
			con.close();
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			response.sendRedirect("http://localhost:8080/finals/InventoryList");
		}
		
	}
	
}